package com.dongss.study.service;

import com.dongss.study.dao.MenuDAO;
import com.dongss.study.domain.Menu;
import com.dongss.study.domain.Role;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * Created by dongshuanshuan on 17-7-30.
 */
@Service
@Transactional
public class MenuService {

    @Resource
    MenuDAO menuDAO;

    public void create(Menu menu){
        menuDAO.save(menu);

        Integer id=menu.getId();
        System.out.println("after save,the id is:"+id);
        if(menu.getParent()!=null){
            String fullIdPath=menu.getParent().getFullIdPath();
            menu.setFullIdPath("["+id+"]"+fullIdPath);

        }else{
            menu.setFullIdPath("["+id+"]");
        }
        menuDAO.update(menu);
    }

    public void update(Menu menu){
        menuDAO.update(menu);

        Integer id=menu.getId();
        if(menu.getParent()!=null){
            String fullIdPath=menu.getParent().getFullIdPath();
            menu.setFullIdPath("["+id+"]"+fullIdPath);

        }else{
            menu.setFullIdPath("["+id+"]");
        }
        menuDAO.update(menu);
    }

    public void delete(Menu menu){
        menuDAO.delete(menu);
    }

    public Menu find(Integer id){
        return menuDAO.get(id);
    }
}
